package com.one.rope.mvp.basecore.util;

import java.util.Collection;
import java.util.List;

/**
 * 数组工具类
 *
 * @author Jinwen.you
 */
public class ArrayUtils {

  /**
   * 空数组
   */
  public static <T> boolean isEmpty(T[] t) {
    return t == null || t.length == 0 ? true : false;
  }

  /**
   * 空列表
   */
  public static <T> boolean isEmpty(Collection<T> list) {
    return list == null || list.size() == 0 ? true : false;
  }

  public static <T> int size(Collection<T> list) {
    return list == null ? 0 : list.size();
  }

  public static <T> T getFirstItem(List<T> list, T defaultValue) {
    if (isEmpty(list)) {
      return defaultValue;
    }
    return list.get(0);
  }

  public static <T> T getLastItem(List<T> list, T defaultValue) {
    if (isEmpty(list)) {
      return defaultValue;
    }
    return list.get(list.size() - 1);
  }

  public static int indexOf(String[] array, String value) {
    return indexOf(array, value, false);
  }

  /**
   * 文本是否包含
   */
  public static boolean isContains(String source, List<String> containsList) {
    if (source == null) {
      return false;
    }
    for (String item : containsList) {
      if (StringUtils.contains(source, item)) {
        return true;
      }
    }
    return false;
  }

  /**
   * 数组中查找一个字符串
   */
  public static int indexOf(String[] array, String value, boolean isIgnoreCase) {
    if (isIgnoreCase) {
      for (int i = 0; i < array.length; ++i) {
        if (array[i].equalsIgnoreCase(value)) {
          return i;
        }
      }
    } else {
      for (int i = 0; i < array.length; ++i) {
        if (array[i].equals(value)) {
          return i;
        }
      }
    }
    return -1;
  }
}
